#coding=utf-8

def PID(val,ex_val,p=0.3,i=0.2,d=1.0,maxVal=0.05):
    '''
    基于本次数值和上次数值返回调整数值
    :param val:
    :param ex_val:
    :param p:
    :param i:
    :param d:
    :return: PID数值
    '''
    #print("{:.4f},{:.4f},{:.4f}".format(val,ex_val,ex_val-val))
    #print("{:.4f},{:.4f},{:.4f}".format(p*val,(ex_val-val)*d,(ex_val+val)*i))

    ret=p*val+(ex_val-val)*d+(ex_val+val)*i
    if ret>maxVal: ret=maxVal
    if ret<-maxVal: ret=-maxVal
    return ret